package com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.entity.EntrustedRelation;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lianhong
 * @since 2022/6/15
 */
@Repository
public class EntrustedRelationDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String deleteEntrustedRelSql = "DELETE FROM sw_entrusted_rel WHERE seqNo IN (:seqNos)";


    public void deleteBy(List<String> seqNos) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("seqNos", seqNos);
        namedParameterJdbcTemplate.update(deleteEntrustedRelSql, params);
    }

    private String insertEntrustedRelSql = "INSERT INTO sw_entrusted_rel (seqNo, consignNoL, agentCode, beginDate, entrustedRel, storageTime, socialCreditCode)" +
            " VALUES (:seqNo, :consignNoL, :agentCode, :beginDate, :entrustedRel, :storageTime, :socialCreditCode)";

    public void save(List<EntrustedRelation> entrustedRelationList) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(entrustedRelationList);
        namedParameterJdbcTemplate.batchUpdate(insertEntrustedRelSql, batch);
    }
}
